home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Random2.0 / Source / R250Engine.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  126 lines

  1. //
  2. // R250Engine
  3. //
  4. // Copyright (C) 1992 Contemporary Design Studios. All rights reserved.
  5. //
  6.  
  7.  
  8. #import "R250Engine.h"
  9. #import <stdlib.h>
  10. #import <stdio.h>
  11.  
  12.  
  13. @implementation R250Engine
  14.  
  15.  
  16. //
  17. // unit
  18. //
  19.  
  20. + (int)unit
  21. {
  22.     return 4;                    // r250 creates 4 byte chunks.
  23. }
  24.  
  25.  
  26. //
  27. // init
  28. //
  29.  
  30. #define SPREAD(num)    (11 * num + 3)
  31.  
  32. - init
  33. {
  34.     unsigned long    mask, diag;
  35.     int            i;
  36.     
  37.     //
  38.     // Insert pseudo-random numbers into the buffer:
  39.     //
  40.     
  41.     for(i = 0; i < 250; i++) {            // Scan through entire array.
  42.         buffer[i] = rand();            //   Put a random number at each location.
  43.     if(rand() > (RAND_MAX / 2))        //   50% Chance that
  44.         buffer[i] |= 0x80000000;        //     we'll OR in the high bit. 
  45.     }
  46.     
  47.     //
  48.     // Guarantee the existance of some specific patterns:
  49.     //
  50.     
  51.     diag = 0x80000000;
  52.     mask = 0xffffffff;
  53.  
  54.     for(i = SPREAD(0); i <= SPREAD(31); i = SPREAD(i)) {
  55.     buffer[i] = (buffer[i] & mask) | diag;    //   Turn diagonal on and left bits off.
  56.     mask >>= 1;                //   Shift mask to new value.
  57.     diag >>= 1;                //   Shift diag to new value.
  58.     }
  59.     
  60.     return self;
  61. }
  62.  
  63.  
  64. //
  65. // makeRandom:
  66. //
  67.  
  68. - makeRandom:(uchar *)storage;
  69. {
  70.     buffer[index] = buffer[index] ^ buffer[(index + 103) % 250];    // Make the next number.
  71.     index = (index + 1) % 250;                        // Increment our index.
  72.     
  73. //    printf("R250Engine: Returning unsigned long %ul.\n", buffer[index]);
  74.     
  75.     *((unsigned long *)storage) = buffer[index];            // Return it to sender.
  76.     
  77.     return self;
  78. }
  79.  
  80.  
  81. //
  82. // read:
  83. //
  84.  
  85. - read:(NXTypedStream *)stream
  86. {
  87.     int        i;
  88.     
  89.     [super write:stream];
  90.     
  91.     NXReadTypes(stream, "i", &index);
  92.     
  93.     for(i = 0; i < 250; i++) {
  94.         NXReadTypes(stream, "i", &(buffer[i]));
  95.     }
  96.     
  97.     return self;
  98. }
  99.  
  100.  
  101. //
  102. // write:
  103. //
  104.  
  105. - write:(NXTypedStream *)stream
  106. {
  107.     int        i;
  108.     
  109.     [super write:stream];
  110.     
  111.     NXWriteTypes(stream, "i", &index);
  112.     
  113.     for(i = 0; i < 250; i++) {
  114.         NXWriteTypes(stream, "i", &(buffer[i]));
  115.     }
  116.     
  117.     return self;
  118. }
  119.  
  120.  
  121. @end
  122.  
  123.  
  124. //
  125. // End of file.
  126. //